<?php

// +----------------------------------------------------------------------
// | ShuipFCMS 联动菜单管理
// +----------------------------------------------------------------------
// | Copyright (c) 2012-2014 http://www.jsy360.com, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 神通教育
// +----------------------------------------------------------------------

namespace Admin\Controller;

use Common\Controller\AdminBase;
use Admin\Service\User;

class CategroyController extends AdminBase
{
    protected function _initialize() {
            if(S('classify')){
                S('classify',null);
            }
    }

    public function grouplist()
    {

        $this->display();
    }

    //地区分类
    public function catecity()
    {
        $result = M('linkage')->where(['parentid' => 0, 'child' => 1])->select();
        $this->assign("categorys", $result);
        $this->display();
    }

    //地区查看下级分类
    public function seek()
    {
        $data = M('linkage')->where(['parentid' => I('get.parentid')])->select();
        $this->assign('data', $data);
        $this->display();
        //dump($data);
    }
    //地区修改
    public function cityedit()
    {
        if(IS_POST){
            $data = I('post.');
            $data['keyid'] = 1;
            $data['style'] = 0;
         // dump($data);exit;
            if(M('linkage')->data($data)->save()){
                $this->success('地区修改成功',U('admin/categroy/catecity'));
            }else{
                $this->error('提示：地区修改失败!');
            }
        }else{
            $data = M('linkage')->where(['linkageid'=>I('get.linkageid')])->find();
            $data['parent'] = M('linkage')->where(['linkageid'=>$data['parentid']])->field('linkageid,name')->find();
            $this->assign('data',$data);
            $this->display();
        }

    }

    //地区添加
    public function addcity()
    {
        if (IS_POST) {
            $data = I('post.');
            $data['keyid'] = 1;
            $data['style'] = 0;
            if(M('linkage')->data($data)->add()){
                $this->success('子地区添加成功',U('admin/categroy/catecity'));
            }else{
                $this->error('提示：子地区添加失败!');
            }
        } else {
            $this->assign('data', I('get.'));
            $this->display();
        }

    }

    //地区删除
    public function delete()
    {
        $linkageid = I('get.linkageid');
        if(M('linkage')->where(['linkageid'=>$linkageid])->delete()){
            $this->success('删除成功',U('admin/categroy/catecity'));
        }else{
            $this->error('提示：删除失败!');
        }
    }



    //其它分类组
    public function otherlist()
    {
        $data = M('category_group')->select();
        $this->assign(['group'=>$data]);
        $this->display();
    }

    //添加分类组
    public function add_group()
    {
        if(IS_POST){
            $list = I('post.');
            $list['g_alias'] = 'job_'.$list['g_alias'];
            if(M('category_group')->add($list)){
                $this->success('分类组添加成功',U('admin/categroy/otherlist'));
            }else{
                $this->error('提示：分类组添加失败!');
            }
        }else{
            $data = M('category_group')->where(['g_alias'=>I('get.alias')])->find();
            $alias = explode('_',$data['g_alias']);
            $data['g_alias'] = $alias[1];

            $this->assign('data',$data);
            $this->display();
        }

    }
    //分类组删除
    public function del_group()
    {
        $alias =I('get.alias');
        if(M('category_group')->where(['g_alias'=>$alias])->delete()){
            $this->success('删除成功！',U('admin/categroy/otherlist'));
        }else{
            $this->error('提示：删除失败!');
        }
    }
    //分类组详情查看
    public function show_category()
    {
        $menu = M('category_group')->where(['g_alias'=>I('get.alias')])->find();
        $data = M('category_menu')->where(['c_alias'=>I('get.alias')])->select();
        $this->assign(['data'=>$data,'menu'=>$menu]);
        $this->display();
    }

    //分组类 分类删除
    public function del_category()
    {
        $id = I('get.id');
        if(M('category_menu')->where(['c_id'=>$id])->delete()){
            $this->success('删除成功!');
        }else{
            $this->error('提示：删除失败!');
        }
    }

    //分组类  分类修改
    public function edit_category()
    {
        if(IS_POST){
            $list = I('post.');
            if(M('category_menu')->where(['c_id'=>$list['c_id']])->save($list)){
                $this->success('分类修改成功',U('admin/categroy/show_category').'&alias='.$list['c_alias']);
            }else{
                $this->error('提示：分类修改失败');
            }
        }else{
            $data = M('category_menu')->where(['c_id'=>I('get.id')])->find();
            $this->assign(['category'=>$data]);
            $this->display();
        }

    }

    //分类组 分类添加
    public function add_category()
    {
        if(IS_POST){
            $list = I('post.');
            $num=0;
            if (is_array($_POST['c_name']) && count($_POST['c_name'])>0)
            {
                for ($i =0; $i <count($_POST['c_name']);$i++){
                    if (!empty($_POST['c_name'][$i]))
                    {
                        $setsqlarr['c_name']=trim($_POST['c_name'][$i]);
                        $setsqlarr['c_alias']=trim($_POST['c_alias'][$i]);
                        $setsqlarr['c_order']=intval($_POST['c_order'][$i]);
                        $setsqlarr['c_index']=getfirstchar($setsqlarr['c_name']);
                        $setsqlarr['c_note']=trim($_POST['c_note'][$i]);
                       if(M('category_menu')->data($setsqlarr)->add()){
                           $num ++;
                       }
                    }

                }

            }
            $this->success('添加成功！共添加'.$num.'个分类',U('admin/categroy/show_category').'&alias='.$setsqlarr['c_alias']);


        }else{
            $data = M('category_group')->where(['g_alias'=>I('get.alias')])->find();
            //dump($data);dump(I('get.alias'));
            $this->assign('group',$data);
            $this->display();
        }

    }


    //职位分类
    public function position()
    {
        $data = M('category_jobs')->where(['parentid' => 0])->order('category_order desc')->select();
        foreach ($data as $key => $v) {
            $data[$key]['sub'] = M('category_jobs')->where(['parentid' => $v['id']])->order('category_order desc')->select();
        }
//        dump($data);exit;
        $this->assign('district', $data);
        $this->display();
    }

    //职位修改
    public function category_jobs_edit()
    {
        if(IS_POST){
            $save = I('post.');
            if(M('category_jobs')->where(['id'=>$save[id]])->data($save)->save()){
                $this->success('修改成功',U('admin/categroy/position'));
            }else{
                $this->error('提示：数据修改失败.');
            }
        }else{
            $memu = M('category_jobs')->where(['parentid'=>0])->select();
            $data = M('category_jobs')->where(['id'=>I('get.id')])->find();
            $this->assign(['data'=>$data,'memu'=>$memu]);
            $this->display();
        }


    }

    //职位添加
    public function category_jobs_add()
    {
        if(IS_POST){

            //新增的入库
            if (is_array($_POST['categoryname']) && count($_POST['categoryname'])>0)
            {
                $num = 0;
                for ($i =0; $i <count($_POST['categoryname']);$i++){
                    if (!empty($_POST['categoryname'][$i]))
                    {
                        $setsqlarr['categoryname']=trim($_POST['categoryname'][$i]);
                        $setsqlarr['category_order']=intval($_POST['category_order'][$i]);
                        $setsqlarr['parentid']=intval($_POST['parentid'][$i]);
                        if(M('category_jobs')->data($setsqlarr)->add()){
                            $num ++;
                        }
                    }

                }
                $this->success('添加成功！本次添加了'.$num."个分类",U('admin/categroy/position'));
            }

        }else{
            $memu = M('category_jobs')->where(['parentid'=>0])->select();
            $this->assign(['memu'=>$memu,'parentid'=>I('get.parentid')]);
            $this->display();
        }
    }

    //AJAX数据加载
    public function ajaxjobs()
    {
        $pid = intval($_POST['pid']);
        $result = M('category_jobs')->where(['parentid' => $pid])->order('category_order desc')->select();

        foreach ($result as $v) {
            $cat[] = $v['id'] . ',' . $v['categoryname'] . ',' . $v['category_order'];
        }
        if (!empty($cat)) {
            exit(implode('|', $cat));
        }
    }

    //职位删除
    public function del_jobs_category()
    {
        $id = $_REQUEST[id];
        if(M('category_jobs')->where(['id'=>$id])->delete())
        {
            $this->success('删除成功！');
        }else{
            $this->error('提示:数据删除失败!');
        }
    }
}
